// components/date-picker-view/index.js
import { getLunar, getZodiac } from './lunarDay'
import { isNull, getStar } from '../../utils/index'

const app = getApp();
const date = new Date()
let minYear = 0
let minMonth = 0
let minDay = 0
let maxYear = 0
let maxMonth = 0
let maxDay = 0
let currentYear= date.getFullYear()
let currentMonth= date.getMonth() + 1
let currentDay= date.getDate()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    pickerView: {
      type: Boolean,
      value: false
    },
		minDate:{
			type: String,
			value: '1950-01-01'
		},
		maxDate:{
			type: String
		},
		value: {
			type: String
		},
		text: String,
    showText: String, // top,bottom
		showStar: Boolean,
    showZodiac: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
		indicatorStyle: `height: ${Math.round(app.globalData.screenHeight/(750/42))}px;`,
    defaultVal: [],
    years: [],
    months: [],
    days: [],
    year: 0,
    month:  0,
    day: 0,
		star: '',
    zodiac: '',
    dateText: '',
    show: ''
  },
  
  ready: function(){
    if(!this.data.pickerView){
      this.setData({
        dateText: "请选择"
      })
    }
		let minDate = this.data.minDate
    let maxDate = isNull(this.data.maxDate) ? currentYear+'-'+currentMonth+'-'+currentDay : this.data.maxDate
    let value = isNull(this.data.value) ? currentYear+'-'+currentMonth+'-'+currentDay : this.data.value
    let showDate, star, year, month, day
		if(isNull(this.data.text)){
			showDate = '请选择'
		}
		if(this.data.showStar && !this.data.textBottom){
			star = '星座'
    }


		let arrMax = maxDate.split('-')
		maxYear = parseInt(arrMax[0])
		maxMonth = parseInt(arrMax[1])
		maxDay = parseInt(arrMax[2])
		let arrMin = minDate.split('-')
		minYear = parseInt(arrMin[0])
		minMonth= parseInt(arrMin[1])
		minDay= parseInt(arrMin[2])
    let arrVal = value.split('-')
    year = parseInt(arrVal[0])
		month = parseInt(arrVal[1])
		day = parseInt(arrVal[2])
    


    this.setData({
      showDate: showDate,
      star: star,
      year: year,
      month: month,
    })

		this.fillYears()
		this.fillMonths(year)
    this.fillDays(year, month)
    this.initDefaultVal(year, month, day)
    this.subChange(year, month, day)

  },

  /**
   * 组件的方法列表
   */
  methods: {
		change(e) {
			const val = e.detail.value
			let year,month,day
			year = this.data.years[val[0]]
			this.fillMonths()
			month = this.data.months[val[1]]
			this.fillDays()
			day = this.data.days[val[2]]
			
			this.setData({
				year: year,
				month: month,
				day: day
			})

			this.subChange(year, month, day)
		}, 
		fillYears() {
      let years=[]
			for (let i = minYear; i <= maxYear; i++) {
				years.push(i)
      }
      this.setData({
        years: years
      })
		}, 
		fillMonths(year) {
      let min = 1
      let max = 12
      if(year == minYear){
				min = minMonth
			}
			if (year == maxYear) {
				max = maxMonth
			}
			let months = []
			for (let i = min; i <= max; i++) {
				months.push(i)
			}
      this.setData({
        months: months
      })
		}, 
		fillDays(year,month) {
			let max = 31
			if (year == maxYear && month == maxMonth) { // 最大月
					max = maxDay
				} else {
				// 根据月份显示不同的天数
				if ([1, 3, 5, 6, 8, 10, 12].includes(month)) { // 一三五七八十腊
					max = 31
				} else if ([4, 6, 9, 11].includes(month)) {
					max = 30
				} else if ([2].includes(month)) {
					if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { // 闰年
						max = 29
					} else { // 平年
						max = 28
					}
				}
			}

			let days = []
			let min = 1
			if(year == minYear && month == minMonth){
				min = minDay
			}
			for (let i = min; i <= max; i++) {
				days.push(i)
      }
      this.setData({
        days: days
      })
		},
		initDefaultVal(year,month,day){
			
			let v1=0,v2=0,v3=0
			for(let i in this.data.years){
				if(this.data.years[i]==year){
					v1=i
					break
				}
			}
			for(let i in this.data.months){
				if(this.data.months[i]==month){
					v2=i
					break
				}
			}
			
			for(let i in this.data.days){
				if(this.data.days[i]==day){
					v3=i
					break
				}
			}
			
			setTimeout(() => {
				//this.defaultVal = [v1, v2, v3]
				this.setData({
					defaultVal: [v1, v2, v3]
				})
			}, 0);
		},
		subChange(year,month,day){
			let m = month
			let d = day
			if(m<10){
				m = '0'+m
			}
			if(d<10){
				d = '0'+d
			}
			let showDate = year + '-' + m + '-' + d
			let lunarDay = getLunar(showDate)
			let lyear = year
			if(month<3){
				if(lunarDay.indexOf('十月')==0 ||lunarDay.indexOf('十一月')==0 ||lunarDay.indexOf('十二月')==0){
					lyear = year -1
				}
			}
			
			let zodiac = getZodiac(lyear)
			let star = getStar(showDate)
			let data = {
				date: showDate,
				star: star,
				lunar: lunarDay,
				zodiac: zodiac
			}
			this.setData({
				showDate: showDate,
				lunarDay: lunarDay,
				zodiac: zodiac,
				star: star
      })
      //this.$emit('pickerData',data)
      if(this.data.pickerView){
        this.triggerEvent('change', data)
      }
    },
    showModal(e){
      this.setData({
        show: 'show'
      })
    },
    hideModal(e){
      this.setData({
        show: ''
      })
    },
    okModal(e){
      let data = {
        date: this.data.showDate,
				star: this.data.star,
				lunar: this.data.lunarDay,
        zodiac: this.data.zodiac
      }
      this.setData({
        show: '',
        dateText: this.data.showDate,
      })
      this.triggerEvent('change', data)
    }
  }
})
